home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr26 / netprog.zip / NETPROG.TAR / net.s5 / udpcli.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  3KB  |  108 lines

  1. /*
  2.  * Example of client using UDP protocol.
  3.  */
  4.  
  5. #include    "inet.h"
  6.  
  7. main(argc, argv)
  8. int    argc;
  9. char    *argv[];
  10. {
  11.     int            tfd;
  12.     struct t_unitdata    unitdata;
  13.     struct sockaddr_in    serv_addr;
  14.  
  15.     pname = argv[0];
  16.  
  17.     /*
  18.      * Open a UDP endpoint.
  19.      */
  20.  
  21.     if ( (tfd = t_open(DEV_UDP, O_RDWR, (struct t_info *) 0)) < 0)
  22.         err_dump("client: can't t_open %s", DEV_UDP);
  23.  
  24.     /*
  25.      * Bind any local address for us.
  26.      */
  27.  
  28.     if (t_bind(tfd, (struct t_bind *) 0, (struct t_bind *) 0) < 0)
  29.         err_dump("client: t_bind error");
  30.  
  31.     /*
  32.      * Initialize a sockaddr_in structure with the address of the
  33.      * the server we want to send datagrams to.
  34.      */
  35.  
  36.     bzero((char *) &serv_addr, sizeof(serv_addr));
  37.     serv_addr.sin_family      = AF_INET;
  38.     serv_addr.sin_addr.s_addr = inet_addr(SERV_HOST_ADDR);
  39.     serv_addr.sin_port        = htons(SERV_UDP_PORT);
  40.  
  41.     /*
  42.      * Now initialize a unitdata structure for sending to the server.
  43.      */
  44.  
  45.     unitdata.addr.maxlen = sizeof(serv_addr);    /* server's addr */
  46.     unitdata.addr.len    = sizeof(serv_addr);
  47.     unitdata.addr.buf    = (char *) &serv_addr;
  48.     unitdata.opt.maxlen  = 0;            /* no options */
  49.     unitdata.opt.len     = 0;
  50.     unitdata.opt.buf     = (char *) 0;
  51.  
  52.     doit(tfd, &unitdata, stdin);    /* do it all */
  53.  
  54.     t_close(tfd);
  55.     exit(0);
  56. }
  57.  
  58. /*
  59.  * Read the contents of the FILE *fp, write each line to the transport
  60.  * endpoint (to the server process), then read a line back from
  61.  * the transport endpoint and print it on the standard output.
  62.  */
  63.  
  64. doit(tfd, sudataptr, fp)
  65. register int        tfd;
  66. struct t_unitdata    *sudataptr;    /* unitdata for sends */
  67. register FILE        *fp;
  68. {
  69.     int            n, flags;
  70.     char            sendline[MAXLINE], recvline[MAXLINE + 1];
  71.     char            *t_alloc();
  72.     struct t_unitdata    *rudataptr;    /* unitdata for receives */
  73.  
  74.     /*
  75.      * Allocate memory for the t_unitdata structure and the address field
  76.      * in that structure.  This allows any size of address to be handled
  77.      * by this function.
  78.      */
  79.  
  80.     rudataptr = (struct t_unitdata *) t_alloc(tfd, T_UNITDATA, T_ADDR);
  81.     if (rudataptr == NULL)
  82.         err_dump("server: t_alloc error for T_UNITDATA");
  83.  
  84.     while (fgets(sendline, MAXLINE, fp) != NULL) {
  85.         n = strlen(sendline);
  86.         sudataptr->udata.len = n;
  87.         sudataptr->udata.buf = sendline;
  88.         if (t_sndudata(tfd, sudataptr) < 0)
  89.             err_dump("client: t_sndudata error");
  90.  
  91.         /*
  92.          * Now read a message from the transport endpoint and
  93.          * write it to our standard output.
  94.          */
  95.  
  96.         rudataptr->opt.maxlen = 0;    /* don't care about options */
  97.         rudataptr->udata.maxlen = MAXLINE;
  98.         rudataptr->udata.buf    = recvline;
  99.         if (t_rcvudata(tfd, rudataptr, &flags) < 0)
  100.             err_dump("client: t_rcvudata error");
  101.         recvline[rudataptr->udata.len] = 0;    /* null terminate */
  102.         fputs(recvline, stdout);
  103.     }
  104.  
  105.     if (ferror(fp))
  106.         err_dump("client: error reading file");
  107. }
  108.